package com.gaozhaoyang.base;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class BaseDao<T> {
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    QueryRunner qr = new QueryRunner(dataSource);

    /*一条数据获取*/
    public T getEntity(String sql, Class claz, Object... params) {
        try {
            return qr.query(sql, new BeanHandler<T>(claz), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*多条数据获取*/
    public List<T> getListEntity(String sql, Class claz, Object... params) {

        try {
            return qr.query(sql, new BeanListHandler<T>(claz), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public int update(String sql,Object...params){
        try {
            return qr.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}